<?php
session_start();
if (isset($_SESSION["username"])) {
    ?>  
    <html>
        <?php
        include 'includes/header.inc.php';
        ?>
        <body>
            <!-- Datenbank -->
            <?php include 'includes/db_connect.inc.php'; ?>
            <div class="site-wrapper">
                <div class="site-wrapper-inner">
                    <div class="cover-container">
                        <div class="inner cover">
                            <h1>BSR Quiz</h1>


                            <?php
                            if (!isset($_SESSION['int_count'])) {
                                $_SESSION['int_count'] = 0;
                            }

                            if (isset($_POST['counter'])) {
                                $text = implode(', ', $_GET['ckbox']);
                                echo $text;
                                $_SESSION['int_count'] ++;
                            }
                            ?>
                            <?php
                            if ($_SESSION['int_count'] <= 10) {
                                ?>
                                <img src="pic/grosskreuz.jpg" class="img-responsive">
                                <form method="post" action="quiz.php">
                                    <input type="hidden" name="sent" value="yes">
                                    <?php
                                    include 'includes/question.inc.php';
                                    ?>
                                    <?php if ($_SESSION['int_count'] != 10) { ?>
                                        <h2><?php echo $Fragen[$_SESSION['int_count']][0]; ?></h2>
                                        <h3><?php echo $Fragen[$_SESSION['int_count']][1]; ?></h3>
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="ckbox[]" id="inlineCheckbox<?php echo $_SESSION['int_count'] . "2"; ?>" value="<?php echo $Fragen[$_SESSION['int_count']][2]; ?>"> <?php echo $Fragen[$_SESSION['int_count']][2]; ?>
                                        </label>
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="ckbox[]" id="inlineCheckbox<?php echo $_SESSION['int_count'] . "3"; ?>" value="<?php echo $Fragen[$_SESSION['int_count']][3]; ?>"> <?php echo $Fragen[$_SESSION['int_count']][3]; ?>
                                        </label>
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="ckbox[]" id="inlineCheckbox<?php echo $_SESSION['int_count'] . "4"; ?>" value="<?php echo $Fragen[$_SESSION['int_count']][4]; ?>"> <?php echo $Fragen[$_SESSION['int_count']][4]; ?>
                                        </label>
                                        <br>
                                        <br>
                                        <input type="submit" name="counter" value="Weiter" class="btn btn-success"> 
                                        <?php
                                        $sent = $_POST['sent'];
                                        $AntwortenCB = $_POST['ckbox'];

                                        if ($sent == 'yes') {
                                            $AntwortPruefen = implode(', ', $AntwortenCB);
                                        }

                                        $AntwortArray = $Fragen[$_SESSION['int_count'] - 1][5];

                                        if ($AntwortArray != "") {
                                            if ($AntwortPruefen == $AntwortArray) {
                                                $_SESSION["Punkte"] ++;
                                            }
                                        }
                                    } else {
                                        echo "<br>";
                                        echo "<br>";
                                        ?>    
                                        <h3>Prüfe jetzt dein Ergebnis</h3><br>
                                        <input type="submit" name="counter" value="Auswerten" class="btn btn-success"> 
                                        <?php
                                        $sent = $_POST['sent'];
                                        $AntwortenCB = $_POST['ckbox'];

                                        if ($sent == 'yes') {
                                            $AntwortPruefen = implode(', ', $AntwortenCB);
                                        }

                                        $AntwortArray = $Fragen[$_SESSION['int_count'] - 1][5];

                                        if ($AntwortArray != "") {
                                            if ($AntwortPruefen == $AntwortArray) {
                                                $_SESSION["Punkte"] ++;
                                            }
                                        }
                                    }
                                } else {
                            echo "<br>";
                            ?> 
                            <h3>Du hast <?php echo $_SESSION["Punkte"]; ?> von 10 Fragen richtig beantwortet!</h3> <?php
                            echo "<br>";
                            echo "<br>";

                            $AEmail = $_SESSION["username"];
                            $PlusPunkte = $_SESSION["Punkte"];
                            $aendernP = "UPDATE quizUser SET Punkte ='$PlusPunkte' WHERE Email = '$AEmail'";
                            $update = mysql_query($aendernP);
                        }
                                ?>

                            </form>
                        </div>
                        <div class="mastfoot">
                            <div class="inner">
                                <p>a FSX-Projekt production powered by Lex & Nyhsel</p>
                                <br> <a href="logout.php">Logout</a><br>
                            </div>
                        </div> 
                    </div> 
                </div> 
            </div> 
        </body>

    </html>
    <?php
} else {
    ?>
    <html>
        <!-- HEADER -->
        <?php
        include 'includes/header.inc.php';
        ?>
        <head>
            <!--Falls eingelogt= weiterleiten auf Quiz -->
            <?php
            include 'includes/session_true_link.php';
            ?>
        </head>

        <body>
            <!-- CONTENT-->
            <div class="site-wrapper">
                <div class="site-wrapper-inner">
                    <div class="cover-container">
                        <div class="inner cover">
                            <h1 class="cover-heading">Bitte loggen sie sich <a href="index.php">hier</a> ein.</h1>

                        </div>
                        <div class="mastfoot">
                            <div class="inner">
                                <p>a FSX-Projekt production powered by Lex & Nyhsel</p>
                            </div>
                        </div>     
                    </div>
                </div>
            </div>
            <?php
            include 'includes/footer.inc.php';
            ?>
        </body>   
    </html>
    <?php
}
?>